package com.toutoushidao.ssm.aliyun;


import com.aliyun.oss.*;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.ObjectMetadata;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

public class AliyunService {
	private OSSClient client;
	private String bucket = null;
	private String adomain = null;


	/**
	 * 上传文件到OSS
	 * 
	 * @param aliFilePath
	 * @param filePath
	 * @param type
	 * @return
	 */
	public boolean uploadToCloud(String aliFilePath, String filePath, String type) {
		return uploadFile2Cloud(aliFilePath, new File(filePath), type);
	}

	/**
	 * 上传文件到OSS
	 * 
	 * @param aliFilePath
	 * @param filePath
	 * @return
	 */
	public boolean uploadToCloud(String aliFilePath, String filePath) {
		String type = getImgType(filePath.substring(filePath.lastIndexOf('.') + 1));
		if(type == null) return false;
		return uploadFile2Cloud(aliFilePath, new File(filePath), type);
	}

	/**
	 * 上传文件到OSS
	 * 
	 * @param aliFilePath
	 * @param file
	 * @param type
	 * @return
	 */
	public boolean uploadToCloud(String aliFilePath, File file, String type) {
		return uploadFile2Cloud(aliFilePath, file, type);
	}

	/**
	 * 删除云端存储的文件Object
	 * 
	 * @param key
	 */
	public void deleteObject(String key) {
		client.deleteObject(bucket, key);
	}

	/**
	 * 下载指定的oss文件到本地
	 * 
	 * @param key
	 * @param filePath
	 * @return
	 */
	public boolean dowloadFile(String key, String filePath) {
		client.getObject(new GetObjectRequest(bucket, key), new File(filePath));
		return true;
	}
	
	/**
	 * 直接复制云端文件
	 * @param sourceKey
	 * @param destinationKey
	 * @return
	 */
	public boolean copyFile(String sourceKey, String destinationKey) {
		client.copyObject(bucket, sourceKey, bucket, destinationKey);
		
		return true;
	}

	/**
	 * 获取阿里云存储的根目录
	 * 
	 * @return
	 */
	public String getAliHost() {
		return "http://" + bucket + "." + adomain + "/";
	}

	/**
	 * 上传OSS共通
	 * 
	 * @param key
	 * @param file
	 * @param type
	 * @return
	 */
	private boolean uploadFile2Cloud(String key, File file, String type) {
		InputStream content;
		try {
			content = new FileInputStream(file);
			// 创建上传Object的Metadata
			ObjectMetadata meta = new ObjectMetadata();

			// 必须设置ContentLength
			meta.setContentLength(file.length());
			if(type != null) {
				meta.setContentType(type);
			}

			// // 上传Object.
			// ensureBucket(client,bucket);
			client.putObject(bucket, key, content, meta);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	/**
	 * 创建Bucket.
	 * 
	 * @param client
	 * @param bucketName
	 * @throws OSSException
	 * @throws ClientException
	 */
	private static void ensureBucket(OSSClient client, String bucketName) throws OSSException, ClientException {
		try {
			// 创建bucket
			client.createBucket(bucketName);
			client.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);
		} catch (ServiceException e) {
			e.printStackTrace();
			if(!OSSErrorCode.BUCKET_ALREADY_EXISTS.equals(e.getErrorCode())) {
				// 如果Bucket已经存在，则忽略
				throw e;
			}
		}
	}


	public static String getImgType(String type) {
		String imgType = null;
		if(type != null) {
			if(type.startsWith(".")) {
				type = type.substring(1);
			}
			if("jpg".equals(type) || "jpeg".equals(type)) {
				imgType = "image/jpeg";
			} else if("png".equals(type)) {
				imgType = "image/png";
			} else if("bmp".equals(type)) {
				imgType = "image/x-ms-bmp";
			} else if("gif".equals(type)) {
				imgType = "image/gif";
			}
		}
		return imgType;
	}


	public void setClient(OSSClient client) {
		this.client = client;
	}
	public String getAdomain() {
		return adomain;
	}
	public void setAdomain(String adomain) {
		this.adomain = adomain;
	}
	public void setBucket(String bucket) {
		this.bucket = bucket;
	}
}
